package handlers

import (
	"context"
	"github.com/gin-gonic/gin"
	log "github.com/sirupsen/logrus"
	"github.com/twwch/gin-sdk/handler"
	"github.com/twwch/gin-sdk/server"
	"net/http"
	cmSvc "twwch/k8s-admin/application/cm"
	"twwch/k8s-admin/models/cm"
	"twwch/k8s-admin/models/common"
)

type ConfigMapHandler struct {
	handler.Base
	cmSvc *cmSvc.ConfigMapService
}

func (h *ConfigMapHandler) Init(ginRouter *gin.RouterGroup) {
	h.Logger = log.WithField("handler", "configMap")
	h.cmSvc = cmSvc.GetService()
	// registry http handler
	if ginRouter != nil {
		appGroup := ginRouter.Group("/cm")
		server.Route(appGroup, http.MethodGet, "/get", h.Get)
		server.Route(appGroup, http.MethodPost, "/create", h.Create)
		server.Route(appGroup, http.MethodPost, "/update", h.Update)
		server.Route(appGroup, http.MethodGet, "/delete", h.Delete)
		server.Route(appGroup, http.MethodGet, "/list", h.List)
		//appGroup.GET("/", warper.CreateHandlerFunc(h.Test, false))
	}
}

func (h *ConfigMapHandler) Get(ctx context.Context, req *cm.GetReq) (*cm.GetResp, error) {
	return h.cmSvc.Get(ctx, req)
}

func (h *ConfigMapHandler) Create(ctx context.Context, req *cm.CreateReq) (*cm.CreateResp, error) {
	return h.cmSvc.Create(ctx, req)
}

func (h *ConfigMapHandler) Update(ctx context.Context, req *cm.UpdateReq) (*cm.UpdateResp, error) {
	return h.cmSvc.Update(ctx, req)
}

func (h *ConfigMapHandler) Delete(ctx context.Context, req *cm.DeleteReq) (*common.Empty, error) {
	return h.cmSvc.Delete(ctx, req)
}

func (h *ConfigMapHandler) List(ctx context.Context, req *cm.ListReq) (*cm.ListResp, error) {
	return h.cmSvc.List(ctx, req)
}
